<!DOCTYPE html>
<html>
{include file='public/header'}
<body>
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="javascript:void(0)">首页</a>
            <a href="javascript:void(0)">财务中心</a>
            <a><cite>快手小程序订单列表</cite></a>
        </span>
        <a class="layui-btn layui-btn-small" id="reload" title="刷新">
            <i class="layui-icon" style="line-height:30px"><i class="iconfont">&#xe6aa;</i></i>
        </a>
    </div>
    <div class="x-body">
        <div class="layui-row">
            <form class="layui-form" lay-filter="searchForm">
                <div class="layui-inline">
                    <label class="layui-form-label w-auto">是否结算：</label>
                    <div class="layui-input-inline">
                        <select name="is_settlement" id="is_settlement">
                            <option value="">请选择</option>
                            <option value="0">否</option>
                            <option value="1">是</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                <label class="layui-form-label w-auto">日期：</label>
                <div class="layui-inline" id="time" style="display: flex;align-items: center;">
                    <div class="layui-input-inline" style="margin-right:10px">
                        <input type="text" class="layui-input" name="start_date" id="start_date" autocomplete="off" placeholder="开始时间">
                    </div>
                    <div class="layui-form-mid">-</div>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="end_date" id="end_date" autocomplete="off" placeholder="结束日期">
                    </div>
                    </div>
                </div>
                <button class="layui-btn" style="margin-left: 10px;" lay-submit lay-filter="searchList">搜索</button>
                <button type="reset" id="resetList" class="layui-btn layui-btn-primary">重置</button>
            </form>
        </div>
        <table id="kwaiAppletOrderList" lay-filter="kwaiAppletOrderList"></table>
    </div>
    <script type="text/html" id="toolbar">
        <div class="layui-btn-container">
            <!-- <button class="layui-btn layui-btn-sm" lay-event="batch_sync">批量同步</button> -->
            <button class="layui-btn layui-btn-sm" lay-event="batch_clear">批量结算</button>
        </div>
    </script>
    <script type="text/html" id="tbBar">
        {{#  if(d.is_settlement === 0 ){ }}
            <a class="layui-btn layui-btn-xs" lay-event="clear">结算</a>
        {{#  } }}
    </script>
    
    <script>
        let totalObj = {}
        let params = {
            elem: '#kwaiAppletOrderList'
            , toolbar: '#toolbar'
            , url: '/admin/finance/kwaiAppletOrderList' //数据接口
            , id: 'kwaiAppletOrderList'
            , limit:15
            , page:true
            , totalRow:true
            , cols: [[ //表头
                { type: 'checkbox' }
                , { field: 'id', title: 'ID', align: 'center', width:90,totalRowText: '总合计：'}
                , { field: 'uid', title: '用户ID', align: 'center',  width:90}
                , { field: 'video_id', title: '短剧ID', align: 'center',  width:75}
                , { field: 'video_name', title: '短剧名称', align: 'center' }
                , { field: 'order_no', title: '订单号',  align: 'center', }
                , { field: 'wechat_order_no', title: '微信/支付宝订单号', width:250, align: 'center', }
                , { field: 'platform_order_no', title: '快手订单号',  align: 'center', }
                , { field: 'pay_type', title: '支付渠道', align: 'center', width:100, templet: function (d) {
                        if (d.pay_type == 1) {
                            return '微信支付';
                        } else if (d.pay_type == 2) {
                            return '支付宝支付';
                        }
                    }
                }
                , { field: 'total_fee', title: '支付金额', align: 'center', width:90 }
                , { field: 'pay_time', title: '支付时间/同步时间', align: 'center', width:165, templet: function (d) {
                        return dayjsFormat(d.pay_time)
                    }
                }
                , { field: 'sync_status', title: '同步状态', align: 'center', width:90, templet: function (d) {
                        return d.sync_status === 1 ? '已同步' : d.sync_status === 2 ? '同步异常' : '未同步'
                    }
                }
                , { field: 'is_settlement', title: '结算状态', align: 'center', width:90, templet: function (d) {
                        return d.is_settlement === 1 ? '已结算' : d.is_settlement === 2 ? '结算异常('+d.settlement_error_msg+')' : '未结算'
                    }
                }
                // , { align: 'center', toolbar: '#tbBar', title: '操作',width:75 }
            ]],
            customTotal:function(){
                let that = this;
                layui.use('form', function () {
                    getTotal('/admin/finance/statisticsKwaiPrice',totalObj).then(val=>{
                        totalObj = val
                    })
                });
            },
            done:function(){
                console.log(totalObj)
                setPrice(totalObj)
            }
        }
        tableList(params) 

        layui.use(['table','laydate'], function () {
            var table = layui.table;
            var laydate = layui.laydate
            table.on('tool(kwaiAppletOrderList)', function (obj) {
                var event = obj.event;
                if (event === 'clear') {
                    AnyAjax('确定要结算该订单吗？','/admin/finance/kwaiOrderSettlement',{ order_no:obj.data.order_no},'kwaiAppletOrderList')
                }
            });
            table.on('toolbar(kwaiAppletOrderList)', function (obj) {
                var event = obj.event;
                var checkStatus = table.checkStatus('kwaiAppletOrderList')
                let arrs = []
                checkStatus.data.forEach(item=>{
                    arrs.push(item.order_no)
                })
                if(checkStatus.data.length === 0){
                    layer.msg('请选择后再操作',{icon:5,time:1000});
                }else{
                    
                    if (event === 'batch_sync') {
                        AnyAjax('确定要同步吗？','/admin/finance/batchOrderReport',{  order_no:arrs.toString() },'kwaiAppletOrderList')
                    } else if (event === 'batch_clear') {
                        AnyAjax('确定要结算吗？','/admin/finance/kwaiOrderSettlement',{  order_no:arrs.toString() },'kwaiAppletOrderList')
                    }
                }
                
            });

            // 日期
            laydate.render({
                elem: '#time',
                range: ['#start_date', '#end_date'],
                format: 'yyyy-MM-dd'
            });
        });

        function setPrice(totalObj){
            $('.layui-table-total td[data-field="total_fee"] .layui-table-cell').text(totalObj.total_price);
        }
    </script>
</body>

</html>